<?php

namespace Ad\Logic;

use common\Factory\Factory;

class baseInputClick {
    
    private function getIP() {
        if (getenv('HTTP_CLIENT_IP')) {
            $ip = getenv('HTTP_CLIENT_IP');
        }
        else if (getenv('HTTP_X_FORWARDED_FOR')) {
            $ip = getenv('HTTP_X_FORWARDED_FOR');
        }
        else if (getenv('HTTP_X_FORWARDED')) {
            $ip = getenv('HTTP_X_FORWARDED');
        }
        else if (getenv('HTTP_FORWARDED_FOR')) {
            $ip = getenv('HTTP_FORWARDED_FOR');
        }
        else if (getenv('HTTP_FORWARDED')) {
            $ip = getenv('HTTP_FORWARDED');
        }
        else {
            $ip = $_SERVER['REMOTE_ADDR'];
        }
        
        return $ip;
    } 
    
    protected function checkIp($channel) {
        $result= true;
        
        if(ENV != 'local') {
            $config = \core\init\init::getConfig();
            $nowIp = $this->getIP();
            $ips = $config['channelCheckIp'][$channel];
            $ipExists = array_search($nowIp, $ips);
            
            $result = $ipExists !== false ? true : false;
            $result === false &&  Factory::getServer('MqWriteLog')->send('channel click check ip failed :' . $config['channelIp'][$channel] .'|'. $nowIp);
        }
        
        return $result;
    }
}